package com.shy.configs.common.events.listeners;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shy.collect.entity.PayWay;
import com.shy.collect.entity.UserInfo;
import com.shy.collect.entity.UserPayConfig;
import com.shy.collect.mapper.PayWayMapper;
import com.shy.collect.mapper.UserInfoMapper;
import com.shy.collect.mapper.UserPayConfigMapper;
import com.shy.configs.common.events.events.MerchConfigMailEvent;
import com.shy.configs.common.events.model.MerchConfigData;
import com.shy.configs.common.mail.MailClient;
import com.shy.configs.common.mail.model.MailProperties;
import jakarta.annotation.Resource;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class MerchConfigMailListener implements ApplicationListener<MerchConfigMailEvent> {

    @Resource
    private PayWayMapper payWayMapper;
    @Resource
    private UserPayConfigMapper userPayConfigMapper;
    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private MailClient mailClient;

    /**
     * 发送邮件给商家
     * <p>
     * 发送给商家的信息:
     * 商户号:
     * 支付产品编码和名称
     * 可以使用的支付方式名称和编码
     * appKey
     * appSecret(签名使用的秘钥)
     * <p>
     * 处理邮件事件
     *
     * @param event
     */
    @Async
    @Override
    public void onApplicationEvent(MerchConfigMailEvent event) {
        MerchConfigData eventData = (MerchConfigData) event.getEventData();

        // 查询用户支付配置user_no,product_code
        QueryWrapper<UserPayConfig> userPayConfigQueryWrapper = new QueryWrapper<>();
        userPayConfigQueryWrapper.eq("user_no", eventData.getUserNo()).eq("product_code", eventData.getProductCode());
        UserPayConfig userPayConfig = userPayConfigMapper.selectOne(userPayConfigQueryWrapper);
        // 查询支付产品对应的支付方式
        QueryWrapper<PayWay> payWayQueryWrapper = new QueryWrapper<>();
        payWayQueryWrapper.eq("pay_product_code", eventData.getProductCode()).orderByAsc("way_code", "sorts");
        List<PayWay> payWays = payWayMapper.selectList(payWayQueryWrapper);

        UserInfo userInfo = userInfoMapper.selectOne(new QueryWrapper<UserInfo>().eq("user_no", eventData.getUserNo()));

        //使用thymeleaf做Html文件，格式化文本， 将html文件作为邮件的附件
        StringBuffer buffer  = new StringBuffer(" ");
        buffer.append("商户号：").append(eventData.getUserNo()).append(";\n")
                .append("商户名称：").append(userPayConfig.getShopName()).append(";\n")
                .append("支付产品编码：").append(eventData.getProductCode()).append(";\n")
                .append("支付产品名称：").append(userPayConfig.getProductName()).append(";\n")
                .append("appKey：").append(userPayConfig.getAppKey()).append(";\n")
                .append("appSecret：").append(userPayConfig.getAppSecret()).append(";\n");

        payWays.forEach(payWay->{
            buffer.append("支付方式：")
                    .append(payWay.getWayCode()).append("[")
                    .append(payWay.getWayName()).append("]").append(";--------------------------------------\n");
        });

        String string = buffer.toString();
        System.out.println("邮件正文信息 = " + string);

        MailProperties prop = new MailProperties();
        prop.setFrom("3403013953@qq.com");
        prop.setTo(new String[]{userInfo.getUserEmail()});
        prop.setSubject("商户支付配置信息");
        prop.setText(buffer.toString());
        boolean send = mailClient.sendSimpleMail(prop);

        //记录日志
        System.out.println("给商家发送信息："+ send +"|"+  buffer.toString());


    }
}
